package com.nic.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.view.View;


import com.nic.view.R;
import com.nic.view.adapter.MultPhotoAdapter;
import com.nic.view.util.Utility;
import com.nic.view.util.glide.GlideUtil;
import com.nic.view.widget.MyGridView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 本地图片多选
 * Created by Administrator on 2018/11/6.
 */

public class PhotoByLocActivity extends VBaseActivity {
    MyGridView myGridView;
    MultPhotoAdapter multPhotoAdapter;
    public static final String TAKE_PHOTO = "take_photo";
    public static final String TAKE_TAG = "Tag";
    public static final String TAKE_LIST = "list";
    public static final String TAKE_PHOTO_ACTION = "com.take.photo";
    public static final int RESULT_CODE = 3242;

    @Override
    public int getContentLayoutId() {
        return R.layout.activity_loc_photo;
    }

    @Override
    public void initView() {
        setToolbarTitle("照片选取");
        getToolbarRightTv().setVisibility(View.VISIBLE);
        getToolbarRightTv().setText("确认");
        getToolbarRightTv().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (multPhotoAdapter.getCheckedPaths().isEmpty()) {
                    finish();
                    return;
                }
//                JLog.d("TAG", "checked->" + multPhotoAdapter.getCheckedPaths());
                Intent acIntent = new Intent(TAKE_PHOTO_ACTION);
                acIntent.putExtra(TAKE_PHOTO, (Serializable) multPhotoAdapter.getCheckedPaths());
                sendBroadcast(acIntent);

                Intent intent = getIntent();
                intent.putExtra(TAKE_PHOTO, (Serializable) multPhotoAdapter.getCheckedPaths());
                setResult(RESULT_CODE, intent);
                finish();
            }
        });
        myGridView = (MyGridView) findViewById(R.id.gridView);
        multPhotoAdapter = new MultPhotoAdapter();
        myGridView.setAdapter(multPhotoAdapter);
        initPhoto();
    }

    private void initPhoto() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                final List<String> list = new ArrayList<String>();
                Utility.getAllPhotoBySdcard(list, "/sdcard");
//                runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        if (!list.isEmpty()) {
//                            multPhotoAdapter.setList(list);
//                            Utility.setListViewHeightBasedOnChildren(myGridView);
//                        }
//                    }
//                });

            }
        }).start();
    }

    public static void jumpTo(Activity activity, int requestCode) {
        Intent intent = new Intent(activity, PhotoByLocActivity.class);
        activity.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void onDestroy() {
        GlideUtil.getInstance().clearImageDiskCache(this);
        super.onDestroy();
    }
}
